package com.Shiliu.dao;

import com.Shiliu.pojo.User;
import com.Shiliu.pojo.User;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class UserDaoImpl  implements com.Shiliu.dao.UserDao {

    public User getLoginUser(Connection connection, String stuNum, String userPassword) throws SQLException {

        System.out.println("走到Userdaoimpl");
        PreparedStatement preparedStatement=null;
        ResultSet resultSet=null;
        User user=null;
        if(connection!=null){
            System.out.println("查询中");
            String sql="select * from user where stuNum=?";
            Object[] params={stuNum};
            user=new User();
            resultSet= BaseDao.execute(connection,sql,preparedStatement,params,resultSet);
            if(resultSet.next()){
                System.out.println("查询有结果");
                user.setId(resultSet.getInt("id"));
                user.setStuNum(resultSet.getString("stuNum"));
                user.getUserName(resultSet.getString("userName"));
                user.setUserPassword(resultSet.getString("userPassword"));
                user.setSex(resultSet.getInt("sex"));
                user.setCollege(resultSet.getString("college"));
                user.setProfession(resultSet.getString("profession"));
                user.setArea(resultSet.getString("area"));
                user.setWechat(resultSet.getString("Wechat"));
                user.setPhone(resultSet.getString("phone"));
                user.setMajor(resultSet.getString("major"));
                user.setType(resultSet.getInt("type"));
                System.out.println(user.getUserPassword());
            }
           BaseDao.release(connection,preparedStatement,resultSet);

            if(!user.getUserPassword().equals(userPassword))
            {
                user=null;
            }
        }

        return  user;
    }


    public  int add(Connection connection, User user) throws Exception {
        PreparedStatement preparedStatement=null;
        int re=0;

        if(connection!=null) {
            System.out.println("准备进行插入语句");
            String stuNum=user.getStuNum();
            String password=user.getUserPassword();
            int sex=user.getSex();
            String college=user.getCollege();
            String profession=user.getProfession();
            String area=user.getArea();
            String Wechat=user.getWechat();
            String phone=user.getPhone();
            String major=user.getMajor();
            int type=user.getType();

            String sql = "insert into user (stuNum,userName,userPassword,sex,college,college,profession,area,Wechat,phone,major,type) values(?,?,?,?)";
            Object[] params = {stuNum,password,sex,college,profession,area,Wechat,phone,major,type};
           re= BaseDao.execute(connection, sql,preparedStatement,params);
            BaseDao.release(connection, preparedStatement, null);

        }
           return re;
    }


    public int modify(Connection connection, User user,int id) throws Exception {
        PreparedStatement preparedStatement=null;
        int re=0;

        if(connection!=null) {
            System.out.println("准备进行修改语句");
            String stuNum=user.getStuNum();
            String password=user.getUserPassword();
            int sex=user.getSex();
            String college=user.getCollege();
            String profession=user.getProfession();
            String area=user.getArea();
            String Wechat=user.getWechat();
            String phone=user.getPhone();
            String major=user.getMajor();
            int type=user.getType();
            String sql = "update user set stuNum=?,userPassword=?,userName=?,sex=?,college=?,profession=?,area=?,Wechat=?,phone=?,major=?,type=? where id = ? ";
            Object[] params = {stuNum,password,sex,college,profession,area,Wechat,phone,major,type,id};
            re= BaseDao.execute(connection, sql,preparedStatement ,params);
            BaseDao.release(connection, preparedStatement, null);

        }
        return re;



    }

    public User getMyInfo(Connection connection,Integer id) throws SQLException {
        PreparedStatement preparedStatement=null;
        ResultSet resultSet=null;
        User user=null;
        if(connection!=null){
            System.out.println("查询中");
            String sql="select * from user where id=?";
            Object[] params={(int)id};
            user=new User();
            resultSet= BaseDao.execute(connection,sql,preparedStatement,params,resultSet);
            if(resultSet.next()){
                System.out.println("查询有结果");
                user.setId(resultSet.getInt("id"));
                user.setStuNum(resultSet.getString("stuNum"));
                user.getUserName(resultSet.getString("userName"));
                user.setUserPassword(resultSet.getString("userPassword"));
                user.setSex(resultSet.getInt("sex"));
                user.setCollege(resultSet.getString("college"));
                user.setProfession(resultSet.getString("profession"));
                user.setArea(resultSet.getString("area"));
                user.setWechat(resultSet.getString("Wechat"));
                user.setPhone(resultSet.getString("phone"));
                user.setMajor(resultSet.getString("major"));
                user.setType(resultSet.getInt("type"));
                System.out.println(user.getUserPassword());
            }
            BaseDao.release(connection,preparedStatement,resultSet);


        }

        return  user;
    }

}
